<template>
    <div class="login_bg">
        <!-- 登录盒子 -->
        <div class="login_box">
            <!-- 头像盒子 -->
            <div class="avatar_box">
                <img src="../../assets/logo.png" alt="">
            </div> 
            <!-- 登录表单部分 --> 
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-yonghu" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont icon-mima" placeholder="请输入密码" type="password"></el-input>
                </el-form-item>
                <!-- 按钮区域 -->
                <el-form-item class="btns">  
                    <el-button type="success" @click="login">登录</el-button> 
                    <el-button type="danger" @click="reset">重置</el-button> 
                </el-form-item>
            </el-form> 
        </div>
    </div>
</template>

<script>
import {requestLogin} from '../../network/index' 
export default {
    name:'Login',
    data(){
        return {
            // 表单的数据绑定对象
            loginForm:{
                username:"admin",
                password:"123456"
            },
            // 表单的验证规则对象
            loginFormRules:{
                username:[
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入用户密码', trigger: 'blur' },
                    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
                ]
            }
        }
    }, 
    methods:{
        // 表单重置
        reset(){
            this.$refs.loginFormRef.resetFields();
        },
        // 表单验证
        login(){ 
            this.$refs.loginFormRef.validate(async valid => { 
                if(!valid) return;
                //方法一：发送网络请求 
                // requestLogin('/login','POSt',this.loginForm)
                // .then(res => {
                //     console.log(res.data);
                //     if(res.data.meta.status !== 200) return this.$message.error("登录失败！");
                //     this.$message.success("登录成功！");
                // // 存储 token 
                //     window.sessionStorage.setItem("token",result.data.token);
                //     this.$router.push('/home'); 
                // }).catch(error => {
                //     console.log(error);
                // });

                // 方法二： 通过解构赋值将 data 从对象中取出来,必须使用 async 和 await 
                const { data:result } = await this.$http.post('/login',this.loginForm);  
                if(result.meta.status !== 200) return this.$message.error("登录失败！")
                this.$message.success("登录成功！"); 
                // 存储 token 
                window.sessionStorage.setItem("token",result.data.token);
                this.$router.push('/home'); 
             })
        }
    }
}

</script>

<style lang="less" scoped>
.login_bg{
  background-color: #2b4b6b;
  height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left:50%;
    transform: translate(-50%,-50%);
    background-color: #ffffff;
    // less 语法嵌套
    .avatar_box{
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
}

.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 10px 20px;
    box-sizing: border-box;
}

.btns{
    display: flex;
    justify-content: flex-end;
}
</style>